﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infragistics.Excel;
using MoneyConvertChinese;
namespace BYManageInfoSystem
{
    public partial class Frm_FacRecvFeeDetail : Form
    {
        private DGBY.BLL.BYDepartTab m_bllBYDepartTab = new DGBY.BLL.BYDepartTab();
        private DGBY.BLL.FactoryDataTab m_bllFactoryDataTab = new DGBY.BLL.FactoryDataTab();
        private DGBY.BLL.FacFeeTab m_bllFacFeeTab = new DGBY.BLL.FacFeeTab();
        private DGBY.BLL.FacFeeRelationTab m_bllFacFeeRelationTab = new DGBY.BLL.FacFeeRelationTab();
        private DGBY.BLL.CargoBoxTab m_bllCargoBoxTab = new DGBY.BLL.CargoBoxTab();
        private DGBY.BLL.BYFeeKaiPiaoCateBankCountTab m_bllBYFeeKaiPiaoCateBankCountTab = new DGBY.BLL.BYFeeKaiPiaoCateBankCountTab();
        private DGBY.BLL.FeeFacInvoiceRelationTab m_bllFeeFacInvoiceRelationTab = new DGBY.BLL.FeeFacInvoiceRelationTab();
        private DGBY.BLL.FeeInvoiceTab m_bllFeeInvoiceTab = new DGBY.BLL.FeeInvoiceTab();
        Decimal m_dTotalBYShouJ = 0;
        Decimal m_dTotalBYTrans = 0;
        Decimal m_dTotalBYZhuangXie = 0;
        string m_strJinEUpper = "";


        public Frm_FacRecvFeeDetail()
        {
            InitializeComponent();
        }
        public void InitUltraWinGridSetting()
        {
            this.ultraGrid1.DisplayLayout.Override.RowSelectors = Infragistics.Win.DefaultableBoolean.True;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorNumberStyle = Infragistics.Win.UltraWinGrid.RowSelectorNumberStyle.VisibleIndex;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorWidth = 40;

        }

        public void LocalPrintPreveiw()
        {
            Infragistics.Shared.ResourceCustomizer rc = Infragistics.Win.Printing.Resources.Customizer;//Resources.Customizer;
            rc.SetCustomizedString("PrintPreview_DialogCaption", "打印预览");
            rc.SetCustomizedString("PrintPreview_Tool_Print", "打印(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_ClosePreview", "关闭(&C)");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuPreviewZoom", "显示比例");
            rc.SetCustomizedString("PrintPreview_Tool_Current_Page", "当前页");
            rc.SetCustomizedString("PrintPreview_Tool_Exit", "退出(&X)");
            rc.SetCustomizedString("PrintPreview_Tool_First_Page", "第一页");
            rc.SetCustomizedString("PrintPreview_Tool_Go_To", "跳至");
            rc.SetCustomizedString("PrintPreview_Tool_Last_Page", "最后一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_Page", "下一页");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_Page", "前一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_View", "下一视图(&N)");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_View", "前一视图(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_Hand_Tool", "手型工具(&H)");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Setup", "页面设置(&U)");
            rc.SetCustomizedString("PrintPreview_Tool_Snapshot_Tool", "快照工具(&S)");
            rc.SetCustomizedString("PrintPreview_Tool_View", "视图(&V)");
            rc.SetCustomizedString("PrintPreview_Tool_Whole_Page", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom", "缩放(&Z)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In", "放大");

            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out", "缩小");

            rc.SetCustomizedString("PrintPreview_ToolCategory_Context_Menus", "上下文菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_File", "文件");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Menus", "菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Tools", "工具栏");
            rc.SetCustomizedString("PrintPreview_ToolCategory_View", "视图");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Zoom_Mode", "缩放模式");

            rc.SetCustomizedString("PrintPreview_ToolTip_ClosePreview", "关闭");
            rc.SetCustomizedString("PrintPreview_ToolTip_Zoom", "缩放");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "页:{0}/{1}");

            rc.SetCustomizedString("CustomizeImg_ToolBar_MenuBar", "菜单");
            rc.SetCustomizedString("CustomizeImg_ToolBar_Standard", "标准");
            rc.SetCustomizedString("CustomizeImg_ToolBar_View", "视图");
            rc.SetCustomizedString("PrintPreview_Tool_File", "文件(&F)");
            rc.SetCustomizedString("PrintPreview_Tool_Tools", "工具(&T)");
            rc.SetCustomizedString("PrintPreview_Tool_Dynamic_Zoom_Tool", "动态缩放工具(&D)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out_Tool", "缩小工具");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In_Tool", "放大工具");
            // rc.SetCustomizedString("PrintPreview_Tool_Page_Layout","菜单");
            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PreviewRowColSelection_SelectedPages", "{0} x {1} 页");

            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Width", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_MarginWidth", "文字宽度");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_PageWidth", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_WholePage", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Layout", "页面布局");
            rc.SetCustomizedString("PrintPreview_Tool_Margin_Width", "文字宽度");
            rc.SetCustomizedString("ContextMenuPreviewHand", "缩放视图");


            rc.SetCustomizedString("PrintPreview_Tool_Reduce_Page_Thumbnails", "缩小");
            rc.SetCustomizedString("PrintPreview_Tool_Show_Page_Numbers", "显示页号");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuThumbnail", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Enlarge_Page_Thumbnails", "放大");
            rc.SetCustomizedString("PrintPreview_Tool_Thumbnails", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Continuous", "连续排序");
            //说明
            rc.SetCustomizedString("StatusBar_DynamicZoom_Instructions", "单击并拖动进行缩放操作");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "当前页: {0} / {1}");
            rc.SetCustomizedString("StatusBar_SnapShot_Instructions", "单击并拖动,系统将选定矩型区域复制到剪帖板");
            rc.SetCustomizedString("StatusBar_ZoomIn_Instructions", "单击并拖动,系统将放大选定矩型区域");
            rc.SetCustomizedString("StatusBar_ZoomOut_Instructions", "单击并拖动,系统将缩小选定矩型区域");
            rc.SetCustomizedString("StatusBar_Hand_Instructions", "单击并拖动以便显示更多内容");
        }
        private void InitCombox()
        {
            DataSet ds = m_bllBYDepartTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxDep.DataSource = ds.Tables[0];
                this.comboBoxDep.DisplayMember = "部门名称";
                this.comboBoxDep.ValueMember = "BYDepartTabID";
                this.comboBoxDep.SelectedIndex = 0;
            }
            ds = m_bllFactoryDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxFac.DataSource = ds.Tables[0];

                this.comboBoxFac.ValueMember = "FactoryDataTabID";
                this.comboBoxFac.DisplayMember = "工厂名称";
                this.comboBoxFac.SelectedIndex = 0;
            }

            ds = m_bllBYFeeKaiPiaoCateBankCountTab.GetAllAccountInfo("BYFeeKaiPiaoCateTab.BYFeeKaiPiaoCateName like " + "'%" + "百业装卸" + "%'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxZhuangXFPName.DataSource = ds.Tables[0];

                this.comboBoxZhuangXFPName.ValueMember = "BYFeeKaiPiaoCateBankCountTabID";
                this.comboBoxZhuangXFPName.DisplayMember = "开户名";
                this.comboBoxZhuangXFPName.SelectedIndex = 0;
            }

            ds = m_bllBYFeeKaiPiaoCateBankCountTab.GetAllAccountInfo("BYFeeKaiPiaoCateTab.BYFeeKaiPiaoCateName like " + "'%" + "运输" + "%'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxTransKaiHuName.DataSource = ds.Tables[0];

                this.comboBoxTransKaiHuName.ValueMember = "BYFeeKaiPiaoCateBankCountTabID";
                this.comboBoxTransKaiHuName.DisplayMember = "开户名";
                this.comboBoxTransKaiHuName.SelectedIndex = 0;
            }

            ds = m_bllBYFeeKaiPiaoCateBankCountTab.GetAllAccountInfo("BYFeeKaiPiaoCateTab.BYFeeKaiPiaoCateName like " + "'%" + "百业收据" + "%'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxBYSJKaiHName.DataSource = ds.Tables[0];

                this.comboBoxBYSJKaiHName.ValueMember = "BYFeeKaiPiaoCateBankCountTabID";
                this.comboBoxBYSJKaiHName.DisplayMember = "开户名";
                this.comboBoxBYSJKaiHName.SelectedIndex = 0;
            }
        }
        private void Frm_FacRecvFeeDetail_Load(object sender, EventArgs e)
        {
            LocalPrintPreveiw();
            InitUltraWinGridSetting();
            InitCombox();
        }
        /// 取得某月的第一天
        /// </summary>
        /// <param name="datetime">要取得月份第一天的时间</param>
        /// <returns></returns>
        private DateTime FirstDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day);
        }
        /// <summary>
        /// 取得某月的最后一天
        /// </summary>
        /// <param name="datetime">要取得月份最后一天的时间</param>
        /// <returns></returns>
        private DateTime LastDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1);
        }
 
        private void buttonImport_Click(object sender, EventArgs e)
        {
            m_dTotalBYShouJ = 0;
            m_dTotalBYTrans = 0;
            m_dTotalBYZhuangXie = 0;
            Decimal dTotalRecvDZ = 0;
            Decimal dTotalRecvOT = 0;
            Decimal dTotalRecvSJ = 0;
            Decimal dTotalRecvBG = 0;
            Decimal dTotalRecvTrans = 0;
            Decimal dTotalRecvPublic = 0;
            Decimal dTotalRecvPrivate = 0;



            Dictionary<string, Decimal> dicFeeName2JinE = new Dictionary<string, decimal>();
            int nTotalGuiCount = 0;
            //List<string> lstFeeName = new List<string>();
            DateTime dtFirst = FirstDayOfMonth(this.dateTimePickerDate.Value);
            DateTime dtEnd = LastDayOfMonth(this.dateTimePickerDate.Value);
            DataSet ds = m_bllFacFeeTab.ShowRecvFacFeeRecEx(@"BYDepartTab.DepartName = '" + this.comboBoxDep.Text + "'"
                                    + " and FacConPeoAndTelTab.FactoryDataTabID = '" + this.comboBoxFac.SelectedValue + "'"
                                     + " and BGDComeBackTab.SBDate >= '" + dtFirst.Date + "'"
                                     + " and BGDComeBackTab.SBDate <= '" + dtEnd.Date + "'");
            DataTable dt = new DataTable();
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                

                dt.Columns.Add("申报日期", System.Type.GetType("System.DateTime"));
                dt.Columns.Add("出口金额", System.Type.GetType("System.Decimal"));

                dt.Columns.Add("合同号", System.Type.GetType("System.String"));
                dt.Columns.Add("柜号", System.Type.GetType("System.String"));
                dt.Columns.Add("柜数", System.Type.GetType("System.Int32"));


                dt.Columns.Add("应收商检费", System.Type.GetType("System.Decimal"));
                dt.Columns.Add("公帐汇款船务费", System.Type.GetType("System.Decimal"));
                dt.Columns.Add("私帐汇款船务费", System.Type.GetType("System.Decimal"));

                dicFeeName2JinE.Add("应收商检费", 0);
                dicFeeName2JinE.Add("公帐汇款船务费", 0);
                dicFeeName2JinE.Add("私帐汇款船务费", 0);

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    int nSelectFacFeeTabID = Convert.ToInt32(ds.Tables[0].Rows[i]["FacFeeTabID"].ToString(), 10);

                    DataSet ds2 = m_bllFacFeeRelationTab.GetFeeNameList("FacFeeTabID = " + nSelectFacFeeTabID.ToString() + " and FeeMoney > 0");

                    if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                    {
                        for (int j = 0; j < ds2.Tables[0].Rows.Count; j++)
                        {

                            string strTemp = ds2.Tables[0].Rows[j]["费用名称"].ToString();
                            if (!dicFeeName2JinE.Keys.Contains(strTemp))
                            {
                                dt.Columns.Add(strTemp, System.Type.GetType("System.Decimal"));
                                dicFeeName2JinE.Add(strTemp, 0);
                                
                            }
                            
                        }
                    }
                }
                dt.Columns.Add("费用合计", System.Type.GetType("System.Decimal"));
            }

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    int nBGDComeBackTabID = Convert.ToInt32(ds.Tables[0].Rows[i]["BGDComeBackTabID"].ToString(), 10);
                    DataSet ds2 = m_bllCargoBoxTab.GetAllCargoBoxInfo("CargoBoxTab.BGDComeBackTabID = " + nBGDComeBackTabID.ToString());
                    if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                    {
                        dr["柜数"] = ds2.Tables[0].Rows.Count;
                        nTotalGuiCount += ds2.Tables[0].Rows.Count;
                        StringBuilder strSql = new StringBuilder();
                        for (int j = 0; j < ds2.Tables[0].Rows.Count; j++)
                        {
                            strSql.Append(ds2.Tables[0].Rows[j]["集装箱号"].ToString());
                            strSql.Append("\n");
                        }
                        dr["柜号"] = strSql.ToString();
                    }
                    else
                    {
                        dr["柜数"] = 0;
                        dr["柜号"] = "";
                    }

                    Decimal dtTotalTemp2 = 0;

                    dr["应收商检费"] = ds.Tables[0].Rows[i]["应收商检金额"];
                    dr["公帐汇款船务费"] = ds.Tables[0].Rows[i]["公帐汇款船务费"];
                    dr["私帐汇款船务费"] = ds.Tables[0].Rows[i]["私帐汇款船务费"];
                    dtTotalTemp2 += (Decimal)ds.Tables[0].Rows[i]["应收商检金额"] + (Decimal)ds.Tables[0].Rows[i]["公帐汇款船务费"] + (Decimal)ds.Tables[0].Rows[i]["私帐汇款船务费"];
                    dicFeeName2JinE["应收商检费"] += (Decimal)ds.Tables[0].Rows[i]["应收商检金额"];
                    dicFeeName2JinE["公帐汇款船务费"] += (Decimal)ds.Tables[0].Rows[i]["公帐汇款船务费"];
                    dicFeeName2JinE["私帐汇款船务费"] += (Decimal)ds.Tables[0].Rows[i]["私帐汇款船务费"];
                    int nSelectFacFeeTabID = Convert.ToInt32(ds.Tables[0].Rows[i]["FacFeeTabID"].ToString(), 10);
                    DataSet ds4 = m_bllFacFeeRelationTab.GetFeeNameList("FacFeeTabID = " + nSelectFacFeeTabID.ToString());

                    if (ds4.Tables.Count > 0 && ds4.Tables[0].Rows.Count > 0)
                    {
                        
                        for (int k = 0; k < ds4.Tables[0].Rows.Count; k++)
                        {
                            string strTemp = ds4.Tables[0].Rows[k]["费用名称"].ToString();
                            if (dicFeeName2JinE.Keys.Contains(strTemp))
                            {

                                dr[strTemp] = ds4.Tables[0].Rows[k]["费用金额"].ToString();
                                dtTotalTemp2 += Convert.ToDecimal(ds4.Tables[0].Rows[k]["费用金额"].ToString());
                                dicFeeName2JinE[strTemp] += Convert.ToDecimal(ds4.Tables[0].Rows[k]["费用金额"].ToString());
                            }


                        }

                        dr["费用合计"] = dtTotalTemp2;
                    }

                    dr["合同号"] = ds.Tables[0].Rows[i]["合同编号"];
                    DateTime dtTemp = ((DateTime)ds.Tables[0].Rows[i]["申报日期"]);
                    dr["申报日期"] = dtTemp.Date;

                    dr["出口金额"] = ds.Tables[0].Rows[i]["报关金额"];

                    dt.Rows.Add(dr.ItemArray);




                    dTotalRecvPublic += (Decimal)ds.Tables[0].Rows[i]["公帐汇款船务费"];
                    dTotalRecvPrivate += (Decimal)ds.Tables[0].Rows[i]["私帐汇款船务费"];


                    //int nSelectFacFeeTabID = Convert.ToInt32(ds.Tables[0].Rows[i]["FacFeeTabID"].ToString(), 10);
                    DataSet ds3 = m_bllFacFeeRelationTab.GetFeeNameList("FacFeeTabID = " + nSelectFacFeeTabID.ToString() + " and FeeCateTab.FeeCateName like " + "'%" + "商检" + "%'");

                    if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
                    {
                        Decimal dtTotalTemp = 0;
                        for (int k = 0; k < ds3.Tables[0].Rows.Count; k++)
                        {
                            string strTemp = ds3.Tables[0].Rows[k]["费用名称"].ToString();
                            if (dicFeeName2JinE.Keys.Contains(strTemp))
                            {
                                dr[strTemp] = ds3.Tables[0].Rows[k]["费用金额"].ToString();
                                dtTotalTemp += Convert.ToDecimal(ds3.Tables[0].Rows[k]["费用金额"].ToString());
                            }


                        }

                        dTotalRecvSJ += (Decimal)ds.Tables[0].Rows[i]["应收商检金额"] +dtTotalTemp;
                    }

                    ds3 = m_bllFacFeeRelationTab.GetFeeNameList("FacFeeTabID = " + nSelectFacFeeTabID.ToString() + " and FeeCateTab.FeeCateName like " + "'%" + "运输" + "%'");

                    if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
                    {
                        Decimal dtTotalTemp = 0;
                        for (int k = 0; k < ds3.Tables[0].Rows.Count; k++)
                        {
                            string strTemp = ds3.Tables[0].Rows[k]["费用名称"].ToString();
                            if (dicFeeName2JinE.Keys.Contains(strTemp))
                            {
                                dr[strTemp] = ds3.Tables[0].Rows[k]["费用金额"].ToString();
                                dtTotalTemp += Convert.ToDecimal(ds3.Tables[0].Rows[k]["费用金额"].ToString());
                            }

                        }

                        dTotalRecvTrans += dtTotalTemp;
                    }

                    ds3 = m_bllFacFeeRelationTab.GetFeeNameList("FacFeeTabID = " + nSelectFacFeeTabID.ToString() + " and FeeCateTab.FeeCateName like " + "'%" + "报关" + "%'");

                    if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
                    {
                        Decimal dtTotalTemp = 0;
                        for (int k = 0; k < ds3.Tables[0].Rows.Count; k++)
                        {
                            string strTemp = ds3.Tables[0].Rows[k]["费用名称"].ToString();
                            if (dicFeeName2JinE.Keys.Contains(strTemp))
                            {
                                dr[strTemp] = ds3.Tables[0].Rows[k]["费用金额"].ToString();
                                dtTotalTemp += Convert.ToDecimal(ds3.Tables[0].Rows[k]["费用金额"].ToString());
                            }

                        }

                        dTotalRecvBG += dtTotalTemp;
                    }
                    ds3 = m_bllFacFeeRelationTab.GetFeeNameList("FacFeeTabID = " + nSelectFacFeeTabID.ToString() + " and FeeCateTab.FeeCateName like " + "'%" + "单证" + "%'");

                    if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
                    {
                        Decimal dtTotalTemp = 0;
                        for (int k = 0; k < ds3.Tables[0].Rows.Count; k++)
                        {
                            string strTemp = ds3.Tables[0].Rows[k]["费用名称"].ToString();
                            if (dicFeeName2JinE.Keys.Contains(strTemp))
                            {
                                dr[strTemp] = ds3.Tables[0].Rows[k]["费用金额"].ToString();
                                dtTotalTemp += Convert.ToDecimal(ds3.Tables[0].Rows[k]["费用金额"].ToString());
                            }

                        }

                        dTotalRecvDZ += dtTotalTemp;
                    }

                    ds3 = m_bllFacFeeRelationTab.GetFeeNameList("FacFeeTabID = " + nSelectFacFeeTabID.ToString() + " and FeeCateTab.FeeCateName like " + "'%" + "其他" + "%'");

                    if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
                    {
                        Decimal dtTotalTemp = 0;
                        for (int k = 0; k < ds3.Tables[0].Rows.Count; k++)
                        {
                            string strTemp = ds3.Tables[0].Rows[k]["费用名称"].ToString();
                            if (dicFeeName2JinE.Keys.Contains(strTemp))
                            {
                                dr[strTemp] = ds3.Tables[0].Rows[k]["费用金额"].ToString();
                                dtTotalTemp += Convert.ToDecimal(ds3.Tables[0].Rows[k]["费用金额"].ToString());
                            }

                        }

                        dTotalRecvOT += dtTotalTemp;
                    }

                    m_dTotalBYShouJ = dTotalRecvDZ + dTotalRecvOT + dTotalRecvPrivate;
                    m_dTotalBYZhuangXie = dTotalRecvSJ + dTotalRecvBG + dTotalRecvPublic;
                    m_dTotalBYTrans = dTotalRecvTrans;
                }
            }

            DataRow drTotal = dt.NewRow();
            drTotal["合同号"] = "合计";
            drTotal["柜数"] = nTotalGuiCount;
            Decimal dTotal = 0;
            foreach (string strTemp in dicFeeName2JinE.Keys)
            {
                drTotal[strTemp] = (Decimal)dicFeeName2JinE[strTemp];
                dTotal += (Decimal)dicFeeName2JinE[strTemp];
            }
            drTotal["费用合计"] = dTotal;
            dt.Rows.Add(drTotal.ItemArray);

            //Decimal dTemp1 = dTotal;
            //Decimal dTemp2 = m_dTotalBYShouJ + m_dTotalBYZhuangXie + m_dTotalBYTrans;
            ultraGrid1.DataSource = dt;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["柜号"].CellMultiLine = Infragistics.Win.DefaultableBoolean.True;


            this.textBoxZhuangXFee.Text = m_dTotalBYZhuangXie.ToString();
            this.textBoxTransFee.Text = m_dTotalBYTrans.ToString();
            this.textBoxBYShouJFee.Text = m_dTotalBYShouJ.ToString();
            this.textBoxPersonName.Text = @"吴婉玲";
            this.textBoxGenDanPerson.Text = @"扬扬";
            

            MoneyConvertChineseEx mcc = new MoneyConvertChineseEx();
            m_strJinEUpper = mcc.MoneyToChinese(dTotal.ToString(), true);
        }

        private void comboBoxTransKaiHuName_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str = this.comboBoxTransKaiHuName.SelectedValue.ToString();
                DataSet ds = m_bllBYFeeKaiPiaoCateBankCountTab.GetAllAccountInfo("BYFeeKaiPiaoCateBankCountTabID = '" + str + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    this.textBoxTransKaiHuBank.Text = ds.Tables[0].Rows[0]["开户行"].ToString();
                    this.textBoxTransKaiHAccount.Text = ds.Tables[0].Rows[0]["帐号"].ToString();
                }
            }
            catch { }
        }

        private void comboBoxZhuangXFPName_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str = this.comboBoxZhuangXFPName.SelectedValue.ToString();
                DataSet ds = m_bllBYFeeKaiPiaoCateBankCountTab.GetAllAccountInfo("BYFeeKaiPiaoCateBankCountTabID = '" + str + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    this.textBoxZhuangXBank.Text = ds.Tables[0].Rows[0]["开户行"].ToString();
                    this.textBoxZhuangXAccount.Text = ds.Tables[0].Rows[0]["帐号"].ToString();
                }
            }
            catch { }
        }

        private void comboBoxBYSJKaiHName_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str = this.comboBoxBYSJKaiHName.SelectedValue.ToString();
                DataSet ds = m_bllBYFeeKaiPiaoCateBankCountTab.GetAllAccountInfo("BYFeeKaiPiaoCateBankCountTabID = '" + str + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    this.textBoxBYShouJBank.Text = ds.Tables[0].Rows[0]["开户行"].ToString();
                    this.textBoxBYShouJAccount.Text = ds.Tables[0].Rows[0]["帐号"].ToString();
                }
            }
            catch { }
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            this.ultraGridPrintDocument1.Grid = this.ultraGrid1;
            this.ultraPrintPreviewDialog1.Document = this.ultraGridPrintDocument1;
            this.ultraPrintPreviewDialog1.ShowDialog(this);
        }

        private void ultraGridExcelExporter1_BeginExport(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.BeginExportEventArgs e)
        {
            DateTime dtFirst = FirstDayOfMonth(this.dateTimePickerDate.Value);
            DateTime dtEnd = LastDayOfMonth(this.dateTimePickerDate.Value);
            
            Worksheet ws = e.CurrentWorksheet;
            
            //ws.Columns[4].Width = 20 * 256;
            //ws.Columns[5].Width = 20 * 256;
            ws.Rows[1].Cells[4].CellFormat.Alignment = Infragistics.Excel.HorizontalCellAlignment.Center;
            ws.Rows[2].Cells[4].CellFormat.Alignment = Infragistics.Excel.HorizontalCellAlignment.Center;

            ws.Rows[1].Cells[4].Value = @"东莞市百业进出口有限公司";
            ws.Rows[2].Cells[4].Value = this.comboBoxFac.Text + @"应收费用明细表";

            ws.Rows[3].Cells[0].Value = @"工厂名称：" + this.comboBoxFac.Text;
            string strFirstTemp = dtFirst.Date.ToShortDateString();
            strFirstTemp = strFirstTemp.Replace(@"-", @"/");
            string strEndTemp = dtEnd.Date.ToShortDateString();
            strEndTemp = strEndTemp.Replace(@"-", @"/");
            ws.Rows[3].Cells[3].Value = @"申报日期：" + strFirstTemp + @"-" + strEndTemp;
            ws.Rows[3].Cells[6].Value = @"跟单人：" + this.textBoxGenDanPerson.Text;

            ws.Rows[4].Cells[0].Value = @"联系人：" + this.textBoxPersonName.Text;
            ws.Rows[4].Cells[3].Value = @"经营单位电话：0769-22423198";
            ws.Rows[4].Cells[6].Value = @"经营单位传真：0769-22429650";
            //e.CurrentWorksheet.Workbook.ActiveWorksheet = ws;

            e.CurrentRowIndex = 5;
            //e.CurrentColumnIndex = 2;
        }

        private void ultraGridExcelExporter1_EndExport(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.EndExportEventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;
            int n = dt.Columns.Count;
            Worksheet ws = e.CurrentWorksheet;
            int a = e.CurrentRowIndex;
            ws.Rows[a].Cells[n-1].Value = m_strJinEUpper;

            ws.Rows[a + 1].Cells[0].Value = "汇款明细";
            int nIndex = a + 2;
            if (!checkBoxTCPubRecvMoney.Checked)
            {
                nIndex++;
                ws.Rows[nIndex].Cells[0].Value = "汇款金额：" + m_dTotalBYTrans.ToString();
                ws.Rows[nIndex].Cells[2].Value = "开户名：" + this.comboBoxTransKaiHuName.Text;
                ws.Rows[nIndex].Cells[5].Value = "开户行：" + this.textBoxTransKaiHuBank.Text;
                ws.Rows[nIndex].Cells[8].Value = "开户帐号：" + this.textBoxTransKaiHAccount.Text;
            }

            if (!checkBoxBYYSPubRecv.Checked)
            {
                nIndex++;
                ws.Rows[nIndex].Cells[0].Value = "汇款金额：" + m_dTotalBYZhuangXie.ToString();
                ws.Rows[nIndex].Cells[2].Value = "开户名：" + this.comboBoxZhuangXFPName.Text;
                ws.Rows[nIndex].Cells[5].Value = "开户行：" + this.textBoxZhuangXBank.Text;
                ws.Rows[nIndex].Cells[8].Value = "开户帐号：" + this.textBoxZhuangXAccount.Text;
            }

            nIndex++;
            ws.Rows[nIndex].Cells[0].Value = "汇款金额：" + m_dTotalBYShouJ.ToString();
            ws.Rows[nIndex].Cells[2].Value = "开户名：" + this.comboBoxBYSJKaiHName.Text;
            ws.Rows[nIndex].Cells[5].Value = "开户行：" + this.textBoxBYShouJBank.Text;
            ws.Rows[nIndex].Cells[8].Value = "开户帐号：" + this.textBoxBYShouJAccount.Text;

            nIndex++;
            ws.Rows[a + 5].Cells[0].Value = "感谢贵司的支持，有您的支持与信任，我们会做得更好！！若有疑问，欢迎来电询问！";
            ws.Rows[a + 6].Cells[0].Value = "请贵公司于5个工作日内回传对账差异，否则我司将视同默认，谢谢！";
        }



        private void ultraGridExcelExporter1_CellExported(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.CellExportedEventArgs e)
        {
            Worksheet ws = e.CurrentWorksheet;
            if (e.CurrentColumnIndex == 2)
            {
                Infragistics.Excel.IWorksheetCellFormat cfCellFmt;
                Infragistics.Excel.IWorksheetCellFormat cfCellFmt2;
                string stringt = "";
                if (e.Value != null)
                {
                    stringt = e.Value.ToString();
                    //e.Value = null;
                }
                if (stringt == "合计")
                {
                    int iRdex = e.CurrentRowIndex;
                    int iCdex = e.CurrentColumnIndex;
                    //ws.Rows[iRdex].Cells[iCdex - 1].Value = "";

                    cfCellFmt = e.CurrentWorksheet.Rows[iRdex].Cells[iCdex - 2].CellFormat;
                    cfCellFmt.LeftBorderStyle =
          Infragistics.Excel.CellBorderLineStyle.None;
                    cfCellFmt2 = e.CurrentWorksheet.Rows[iRdex].Cells[iCdex + 1].CellFormat;
                    cfCellFmt2.RightBorderStyle =
          Infragistics.Excel.CellBorderLineStyle.None;

                    // Create a merged region that will be a header to the column headers
                    Infragistics.Excel.WorksheetMergedCellsRegion mergedRegion1 =
                      ws.MergedCellsRegions.Add(iRdex, iCdex - 2, iRdex, iCdex + 1);

                    // Set the value of the merged region
                    mergedRegion1.Value = stringt;

                    // Give the merged region a solid background color
                    mergedRegion1.CellFormat.FillPattern =
                      Infragistics.Excel.FillPatternStyle.Solid;
                    mergedRegion1.CellFormat.FillPatternForegroundColor = Color.White;

                    // Set the cell alignment of the middle cell in the merged region.
                    // Since a cell and its merged region shared a cell format, this will 
                    // ultimately set the format of the merged region
                    ws.Rows[iRdex].Cells[iCdex].CellFormat.Alignment =
                      Infragistics.Excel.HorizontalCellAlignment.Center;
                }

            }
        }

        private void ultraGridExcelExporter1_HeaderRowExporting(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.HeaderRowExportingEventArgs e)
        {
            //e.CurrentColumnIndex = 2;
            //Worksheet ws = e.CurrentWorksheet;
            //ws.Rows[e.GridRow.Index].Cells[0].Value = e.GridRow.Index + 1;
        }

        private void SetTCRecvMoneyPrivate(int nPrivate)
        {
                        
            DateTime dtFirst = FirstDayOfMonth(this.dateTimePickerDate.Value);
            DateTime dtEnd = LastDayOfMonth(this.dateTimePickerDate.Value);
            DataSet ds = m_bllFacFeeTab.ShowRecvFacFeeRecEx(@"BYDepartTab.DepartName = '" + this.comboBoxDep.Text + "'"
                        + " and FacConPeoAndTelTab.FactoryDataTabID = '" + this.comboBoxFac.SelectedValue + "'"
                         + " and BGDComeBackTab.SBDate >= '" + dtFirst.Date + "'"
                         + " and BGDComeBackTab.SBDate <= '" + dtEnd.Date + "'");

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    int nSelectFacFeeTabID = Convert.ToInt32(ds.Tables[0].Rows[i]["FacFeeTabID"].ToString(), 10);
                    DataSet ds2 = m_bllFeeFacInvoiceRelationTab.GetFeeFacInvoiceRelationList("FacFeeTabID = '" + nSelectFacFeeTabID.ToString() + "'");
                    if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                    {
                        for (int j = 0; j < ds2.Tables[0].Rows.Count; j++)
                        {
                            string strTemp = ds2.Tables[0].Rows[j]["开票明细分类名"].ToString();
                            int nIndex = strTemp.IndexOf("运输");
                            if (nIndex >= 0)
                            {
                                int nFeeInvoiceTabID = Convert.ToInt32(ds2.Tables[0].Rows[j]["FeeInvoiceTabID"].ToString(), 10);
                                DGBY.Model.FeeInvoiceTab modelFeeInvoiceTab = m_bllFeeInvoiceTab.GetModel(nFeeInvoiceTabID);
                                modelFeeInvoiceTab.IsPrivate = nPrivate;
                                m_bllFeeInvoiceTab.Update(modelFeeInvoiceTab);
                            }

                        }
                    }
                }
            }

        }


        private void SetZXRecvMoneyPrivate(int nPrivate)
        {

            DateTime dtFirst = FirstDayOfMonth(this.dateTimePickerDate.Value);
            DateTime dtEnd = LastDayOfMonth(this.dateTimePickerDate.Value);
            DataSet ds = m_bllFacFeeTab.ShowRecvFacFeeRecEx(@"BYDepartTab.DepartName = '" + this.comboBoxDep.Text + "'"
                        + " and FacConPeoAndTelTab.FactoryDataTabID = '" + this.comboBoxFac.SelectedValue + "'"
                         + " and BGDComeBackTab.SBDate >= '" + dtFirst.Date + "'"
                         + " and BGDComeBackTab.SBDate <= '" + dtEnd.Date + "'");

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    int nSelectFacFeeTabID = Convert.ToInt32(ds.Tables[0].Rows[i]["FacFeeTabID"].ToString(), 10);
                    DataSet ds2 = m_bllFeeFacInvoiceRelationTab.GetFeeFacInvoiceRelationList("FacFeeTabID = '" + nSelectFacFeeTabID.ToString() + "'");
                    if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
                    {
                        for (int j = 0; j < ds2.Tables[0].Rows.Count; j++)
                        {
                            string strTemp = ds2.Tables[0].Rows[j]["开票明细分类名"].ToString();
                            int nIndex = strTemp.IndexOf("装卸");
                            if (nIndex >= 0)
                            {
                                int nFeeInvoiceTabID = Convert.ToInt32(ds2.Tables[0].Rows[j]["FeeInvoiceTabID"].ToString(), 10);
                                DGBY.Model.FeeInvoiceTab modelFeeInvoiceTab = m_bllFeeInvoiceTab.GetModel(nFeeInvoiceTabID);
                                modelFeeInvoiceTab.IsPrivate = nPrivate;
                                m_bllFeeInvoiceTab.Update(modelFeeInvoiceTab);
                            }

                        }
                    }
                }
            }

        }

        private void checkBoxTCPubRecvMoney_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBoxTCPubRecvMoney.Checked)
            {
                SetTCRecvMoneyPrivate(1);
                this.textBoxTransFee.Enabled = false;
                this.comboBoxTransKaiHuName.Enabled = false;
                this.textBoxTransKaiHuBank.Enabled = false;
                this.textBoxTransKaiHAccount.Enabled = false;
                if (this.textBoxTransFee.Text != "" && this.textBoxBYShouJFee.Text != "")
                {
                    Decimal dtTemp = Convert.ToDecimal(this.textBoxTransFee.Text);
                    Decimal dtTemp2 = Convert.ToDecimal(this.textBoxBYShouJFee.Text);
                    Decimal dtTotal = dtTemp + dtTemp2;
                    m_dTotalBYShouJ = dtTotal;
                    this.textBoxBYShouJFee.Text = dtTotal.ToString();
                }
            }
            else
            {
                SetTCRecvMoneyPrivate(0);
                this.textBoxTransFee.Enabled = true;
                this.comboBoxTransKaiHuName.Enabled = true;
                this.textBoxTransKaiHuBank.Enabled = true;
                this.textBoxTransKaiHAccount.Enabled = true;
                if (this.textBoxTransFee.Text != "" && this.textBoxBYShouJFee.Text != "")
                {
                    Decimal dtTemp = Convert.ToDecimal(this.textBoxTransFee.Text);
                    Decimal dtTemp2 = Convert.ToDecimal(this.textBoxBYShouJFee.Text);
                    Decimal dtTotal = dtTemp2 - dtTemp;
                    m_dTotalBYShouJ = dtTotal;
                    this.textBoxBYShouJFee.Text = dtTotal.ToString();
                }
            }
        }

        private void checkBoxBYYSPubRecv_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxBYYSPubRecv.Checked)
            {
                SetZXRecvMoneyPrivate(1);
                this.textBoxZhuangXFee.Enabled = false;
                this.comboBoxZhuangXFPName.Enabled = false;
                this.textBoxZhuangXBank.Enabled = false;
                this.textBoxZhuangXAccount.Enabled = false;
                if (this.textBoxZhuangXFee.Text != "" && this.textBoxBYShouJFee.Text != "")
                {
                    Decimal dtTemp = Convert.ToDecimal(this.textBoxZhuangXFee.Text);
                    Decimal dtTemp2 = Convert.ToDecimal(this.textBoxBYShouJFee.Text);
                    Decimal dtTotal = dtTemp + dtTemp2;
                    m_dTotalBYShouJ = dtTotal;
                    this.textBoxBYShouJFee.Text = dtTotal.ToString();
                }
            }
            else
            {
                SetZXRecvMoneyPrivate(0);
                this.textBoxZhuangXFee.Enabled = true;
                this.comboBoxZhuangXFPName.Enabled = true;
                this.textBoxZhuangXBank.Enabled = true;
                this.textBoxZhuangXAccount.Enabled = true;
                if (this.textBoxZhuangXFee.Text != "" && this.textBoxBYShouJFee.Text != "")
                {
                    Decimal dtTemp = Convert.ToDecimal(this.textBoxZhuangXFee.Text);
                    Decimal dtTemp2 = Convert.ToDecimal(this.textBoxBYShouJFee.Text);
                    Decimal dtTotal = dtTemp2 - dtTemp;
                    m_dTotalBYShouJ = dtTotal;
                    this.textBoxBYShouJFee.Text = dtTotal.ToString();
                }
            }
        }
    }
}
